extends Path2D

# 动画相关
var ANIM_TICK = 0.15 # 动画单位时间
var MAX_GAP = 0.13 #卡牌最大间距
var GIANT_GAP_LEFT = 0.16 # 悬停时左侧间距
var GIANT_GAP_RIGHT = 0.16 # 悬停时右侧间距
var start_ratio # path上的起始位置，0~1
var gap # 目前使用的间距
var path_follower_list = [] # 卡牌挂载的pathfollower的list
var path_follower_base_ratio = [] # 上述list中的点的位置list

# 卡组相关
const MAX_CARD_COUNT = 10 # 最大手牌上限
var draw_pile = [] # 抽牌堆
var hand_pile = [] # 手牌
var discard_pile = [] # 弃牌堆
var burned_pile = [] # 消耗牌堆
var full_group = []

var my_player

func _ready():
	my_player = get_parent().get_parent()
	# 给卡组中放一些卡
	for i in range(7):
		var new_card = preload("res://Card/Cards/Card001_attack.tscn").instantiate()
		full_group.append(new_card)
	# 将卡组里的牌都放入抽牌堆
	for card in full_group:
		draw_pile.append(card)
	# 洗牌
	draw_pile.shuffle()
	print("deck ready")

# ============================ 卡牌逻辑部分 ===============================

# 抽卡
func draw_card(draw_card_number):
	for i in range(draw_card_number):
		if len(hand_pile) + 1 > MAX_CARD_COUNT:
			Messager.add_message("手牌已满")
			break
		else:
			if len(draw_pile) == 0:
				shuffle_and_redraw()
				if len(draw_pile) == 0:
					Messager.add_message("牌堆已空")
					break
			# 逻辑层上，在手牌中追加抽牌堆中的牌
			var pop_card = draw_pile.pop_front()
			hand_pile.append(pop_card)
			pop_card.my_player = my_player
			# UI层上，新建pathfollower，然后挂载卡牌
			var path_follower = PathFollow2D.new()
			add_child(path_follower)
			path_follower_list.append(path_follower)
			path_follower.add_child(pop_card)
			# TODO 动画
			card_reset_arrange()

# 弃牌
func discard_card(index):
	# 逻辑层上，把弃的牌移动到弃牌堆
	var pop_card = hand_pile.pop_at(index)
	path_follower_list.pop_at(index)
	discard_pile.append(pop_card)
	# UI层上，先把卡牌拿下来，然后将pathfollower删除
	var pop_path_follower = pop_card.get_parent()
	pop_path_follower.remove_child(pop_card)
	pop_path_follower.queue_free()
	# TODO 动画
	card_reset_arrange()

# 弃所有牌
func discard_all():
	# 逻辑层上，将手牌全部移动到弃牌堆中
	for card in hand_pile:
		discard_pile.append(card)
	hand_pile = []
	# UI层，脱离所有手牌，然后删除pathfollower
	for path_follower in path_follower_list:
		path_follower.remove_child(path_follower.get_child(0))
		path_follower.queue_free()
	path_follower_list = []
	# TODO 动画

# 将弃牌堆中的牌打乱，放入抽牌堆
func shuffle_and_redraw():
	discard_pile.shuffle()
	draw_pile = discard_pile.duplicate(true)
	discard_pile = []

# ============================ 动画部分 ===============================
# 卡牌排布 Reset 动画
func card_reset_arrange():
	# 给卡牌重新编号
	var t = 0
	for follower in path_follower_list:
		follower.get_child(0).my_index = t
		t += 1
	# 重新计算位置
	cal_start_pos_and_gap()
	# tween移动至新位置
	for i in range(len(hand_pile)):
		var tween = get_tree().create_tween()
		tween.tween_property(path_follower_list[i], "progress_ratio", start_ratio + gap * i, ANIM_TICK)

# 计算卡牌排布的开始位置和间隔
func cal_start_pos_and_gap():
	if (len(hand_pile) - 1) * MAX_GAP <= 0.98: # 如果牌的数量较少，可以展开排布
		start_ratio = (1 - (len(hand_pile) - 1) * MAX_GAP) / 2.0
		gap = MAX_GAP
	else: # 如果牌多，需要紧凑排布
		start_ratio = 0.01
		gap = 0.98 / (len(hand_pile) - 1)
	# 基准ration重置
	path_follower_base_ratio.clear()
	for i in range(len(hand_pile)):
		path_follower_base_ratio.append(start_ratio + gap * i)

# 有卡牌被悬停时，重新排布卡牌
func card_enter_rearrange_for(index):
	var avg_gap
	# 对悬停卡前面的卡的rearrange
	if index > 1:
		avg_gap = (path_follower_base_ratio[index] - GIANT_GAP_LEFT - 0.01) / (index - 1)
	else: # 如果悬停的是第二张卡
		avg_gap = 1

	if index > 0: # 如果悬停的是第一张卡，则不考虑前面的卡牌的rearrange
		for i in range(index):
			var new_ratio = max(path_follower_base_ratio[index] - GIANT_GAP_LEFT - (index - i - 1) * min(avg_gap, MAX_GAP), 0.01)
			var tween = get_tree().create_tween()
			tween.tween_property(path_follower_list[i], "progress_ratio", new_ratio, ANIM_TICK)

	# 对悬停卡后面的卡rearrange
	if index < len(hand_pile) - 2:
		avg_gap = (1 - path_follower_base_ratio[index] - GIANT_GAP_RIGHT - 0.01) / (len(hand_pile) - index - 2)
	else: # 如果悬停的是倒数第二张卡
		avg_gap = 1

	if index < len(hand_pile) - 1: # 如果悬停的是最后一张卡，则不考虑后面卡牌的rearrange
		for i in range(index + 1, len(hand_pile)):
			var new_ratio = min(path_follower_base_ratio[index] + GIANT_GAP_RIGHT + (i - index - 1) * min(avg_gap, MAX_GAP), 0.99)
			var tween = get_tree().create_tween()
			tween.tween_property(path_follower_list[i], "progress_ratio", new_ratio, ANIM_TICK)
